#include "performerslist.h"
#include <QLabel>
#include <QDebug>

PerformersList::PerformersList(UserList *list, QWidget *parent) :
    QGroupBox(parent),m_list(list)
{
    setTitle(tr("Ответственные за исполнение"));
    main = new QVBoxLayout;
    setLayout(main);
}

void PerformersList::appendPerformer(const int &index)
{
    UsersListWidget* view = new UsersListWidget(m_list,this);
    connect(view,SIGNAL(currentIndexChanged(int)),
            this,SLOT(currentIndexChanged(int)));
    view->setObjectName(tr("%1").arg(m_views.count()));
    view->setUser(index);
    int val=m_views.count();
    m_views.append(view);

    for(int i=0;i<m_views.count();i++)
    {
        if(i==val) continue;
        m_views.at(i)->uninclude(m_views.at(val)->itemData(m_views.at(val)->currentIndex()).toInt());
        m_views.at(val)->uninclude(m_views.at(i)->itemData(m_views.at(i)->currentIndex()).toInt());
    }
    main->addWidget(view);
    emit sizeChange(m_views.count());
}

void PerformersList::removePerformer()
{
    if(m_views.count()>0)
    {
        delete m_views.takeAt(m_views.count()-1);
        emit sizeChange(m_views.count());
    }
}

void PerformersList::appendPerformer()
{
    UsersListWidget* view = new UsersListWidget(m_list,this);
    connect(view,SIGNAL(currentIndexChanged(int)),
            this,SLOT(currentIndexChanged(int)));
    view->setObjectName(tr("%1").arg(m_views.count()));
    int val=m_views.count();
    m_views.append(view);
    for(int i=0;i<m_views.count();i++)
    {
        if(i==val) continue;
        m_views.at(i)->uninclude(m_views.at(val)->itemData(m_views.at(val)->currentIndex()).toInt());
        m_views.at(val)->uninclude(m_views.at(i)->itemData(m_views.at(i)->currentIndex()).toInt());
    }
    main->addWidget(view);
    emit sizeChange(m_views.count());
    emit performerChange(val,view->itemData(view->currentIndex()).toInt());
}

void PerformersList::removePerformer(const int &view)
{
   delete m_views.takeAt(view);
   emit sizeChange(m_views.count());
}

void PerformersList::currentIndexChanged(const int &index)
{
    int val=sender()->objectName().toInt();
    int id=m_views.at(val)->itemData(index).toInt();
    for(int i=0;i<m_views.count();i++)m_views.at(i)->refresh();
    for(int i=0;i<m_views.count();i++)
        for(int k=0;k<m_views.count();k++)
            if(i!=k) m_views.at(i)->uninclude(m_views.at(k)->itemData(m_views.at(k)->currentIndex()).toInt());
    emit performerChange(val,id);
}
